<?php

namespace app\admin\controller;

use think\Controller;
use app\common\model\SystemUser;
use buildView\Button;
use buildView\Form;
use buildView\Grid;
use app\common\model\ShopStore;
use app\common\model\ShopSchoolRegion;
use app\common\model\ShopStoreDevice;
use think\Db;

/**
 * 门店管理
 * Class Store
 * @package app\admin\controller
 */
class Store extends Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $grid = new Grid(new ShopStore);
        $grid->logo('logo')->width(150)->image(5,120);
        $grid->setTitle('门店列表');
        $grid->column('name','门店名称');
		$grid->column('store_area','门店面积(m²)');
		$grid->column('devices_number','设备数量');
        $grid->column('address_info', '门店地址')->display(function ($val, $data) {
            $html = "{$data['province']}-{$data['city']}-{$data['area']}-{$data['address']}";
            return $html;
        })->width(200)->align('left');
        $grid->column('info', '营业时间')->display(function ($val, $data) {
            $html = "{$data['start_time']}--{$data['end_time']}";
            return $html;
        })->align('left');
        $grid->column('status', '状态')->width(150)->align('left')->switchs([
            'on' => ['text' => '启用', 'value' => 1],
            'off' => ['text' => '禁用', 'value' => 0],
        ]);
		$grid->column('create_at','添加时间');
		$grid->filter(function ($filter){
            $filter->like('name','门店名称');
            $filter->dateBetween('create_at','添加时间');
        });
        $grid->hideExportButton();
         $grid->actions(function ($action,$data){
            if ($data['admin_id'] == 0) {
                $button3 = new Button('绑定','normal');
                $button3->href(url('updateAct') . '?id=' . $data['id'], 'modal');
                $action->prepend($button3);
            }else{
                $button3 = new Button('解绑','danger');
                $button3->save($data['id'], ['admin_id' => 0,'sys_id' => $data['admin_id']],'admin/store/delectAct','是否解除绑定？');
                $action->prepend($button3);
            }
            $action->hideDetail();
            
            $button3 = new Button('分类','');
            $button3->href(url('Admin/StoreCate/index') . '?store_id=' . $data['id']);
            $action->prepend($button3);
            
            $button4 = new Button('商品','');
            $button4->href(url('Admin/StoreGoods/index') . '?store_id=' . $data['id']);
            $action->prepend($button4);
        });
        return $grid->view();
    }

    /**
     * 添加/编辑
     * @auth true
     * @menu true
     * @return string
     */
    public function form()
    {
        $form = new Form(new ShopStore);
		$form->text('name','门店名称')->required();
		$form->image('logo','logo')->required();
        $form->text('phone','手机号码')->required();
        $form->image('contract_img','签约合同')->multiple()->required();
		$form->number('store_area','门店面积(m²)')->required();
		$form->number('freight','配送费')->required();

        $deviceDate = array(['id'=>0,'text'=>'无','son'=>array(['id'=>0,'text'=>'无'])],['id'=>1,'text'=>'有','son'=>array(['id'=>1,'text'=>'有'])]);
        $form->selectGroup('shoe', '洗鞋机')->options($deviceDate)->layui('inline')->md(4)->required();
		
		$form->number('devices_number','设备数量')->required();
        $form->timeRange(['start_time','end_time'],'营业时间')->required();
        $region = ShopSchoolRegion::field('id,title as text,pid')->where('pid', 0)->with(['son' => function ($q) {
            $q->field('id,title as text,pid');
        }])->select();
        $form->selectGroup('region_id', '选择区域')->options($region)->layui('inline')->md(4)->required();
		$form->text('address','详情地址');
        $form->map(['latitude','longitude','opsition'],'地图位置');
        $form->text('remark','备注');
        $form->saving(function ($post) {
            $store = ShopStore::where('id',$post['id'])->find();
            if(!empty($store['longitude'])){
                $post['longitude'] = $store['longitude'];
                $post['latitude'] = $store['latitude'];
//                halt(1231465);
            }
            $res = ShopSchoolRegion::where('status',1)->where('title',$post['name'])->find();
            $area = ShopSchoolRegion::where('status',1)->where('id',$post['region_id'])->find();
            $post['area'] = $area['title'];
            $city = ShopSchoolRegion::where('status',1)->where('id',$area['pid'])->find();
            $post['city'] = $city['title'];
            if (!$res){
                $data = [
                    'pid' => $post['region_id'],
                    'title' => $post['name'],
                    'level' => 3
                ];
                ShopSchoolRegion::create($data);
            }
            return $post;
        });
        return $form->view();
    }
    
    /**
     * 绑定管理用户
     * @auth true
     * @menu true
     */
    public function updateAct()
    {
        $form = new Form(new ShopStore());
        $user = SystemUser::where('is_open',0)->where('region_id',null)->select()->column('search_tip','id');
        $form->select('admin_id','管理用户')->options($user)->required();
        $form->saving(function ($post) {
            SystemUser::where('id',$post['admin_id'])->update(['is_open'=>1,'store_id'=>$post['id']]);
            return $post;
        });
        return $form->view();
    }

    /**
     * 解绑管理用户
     * @auth true
     * @menu true
     */
    public function delectAct()
    {
        $id = $this->request->param('id');
        $admin_id = $this->request->param('sys_id');
        ShopStore::where('id',$id)->setField('admin_id',0);
        SystemUser::where('id',$admin_id)->update(['is_open'=>0,'store_id'=>0]);
        $this->success('成功');
    }
}
